<template>
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      :rules="formRules"
      ref="queryFormRef"
      :inline="true"
      label-width="88px"
    >
      <el-form-item label="账单日期" prop="searchTime">
        <el-date-picker
          v-model="queryParams.searchTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>


      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>

        <el-button
          type="success"
          plain
          @click="exportExcel"
          :loading="exportLoading"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table border v-loading="loading" :show-overflow-tooltip="true"
              :data="list" :row-class-name="tableRowClassName"
              style="width: 100%;" :height="tableHeight"
              id="bill-table">
      <el-table-column
        header-align="center"
        align="center"
      >
        <template #header>
          <span style="font-size: 22px;line-height: 24px;color: #000000">微信医保移动支付对账单  ({{queryParams.searchTime[0]}} ~ {{queryParams.searchTime[1]}}) </span>
        </template>

        <el-table-column
          prop="title"
          header-align="center"
          align="center"
          min-width="170px"
          show-overflow-tooltip="true"
          label="业务类型"
        >
          <template #default="scope">
            <span v-html="titleFormatter(scope.row.title)"></span>
          </template>
        </el-table-column>
        <el-table-column
          prop="BRXM"
          header-align="center"
          align="center"
          width="70px"
          label="病人姓名"/>
        <el-table-column
          prop="BRID"
          header-align="center"
          align="center"
          width="80px"
          label="病人ID"/>
        <el-table-column
          prop="orderNo"
          header-align="center"
          align="center"
          min-width="190px"
          label="平台订单号">
          <template #default="scope">
            <span v-if="scope.row.refundId && scope.row.refundId !== ''"> {{ scope.row.channelNo }}</span>
            <span v-else> {{ scope.row.orderNo }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="orderId"
          header-align="center"
          align="center"
          min-width="160px"
          label="支付订单号"/>
        <el-table-column
          prop="channelNo"
          header-align="center"
          align="center"
          min-width="150px"
          show-overflow-tooltip="true"
          label="渠道订单号">
          <template #default="scope">
            <span v-if="scope.row.refundId && scope.row.refundId !== ''"> {{ scope.row.orderNo }}</span>
            <span v-else> {{ scope.row.channelNo }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="chsNo"
          header-align="center"
          align="center"
          min-width="230px"
          label="医保订单号"/>
        <el-table-column
          prop="payAmount"
          header-align="center"
          align="center"
          min-width="85px"
          label="总金额">
          <template #default="scope">
            <span style=" color: red;"  v-if="scope.row.payAmount && scope.row.payAmount !== ''"> {{ scope.row.payAmount }}</span>
            <span style=" color: red;"  v-else> {{ scope.row.amount }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="cashAmount"
          header-align="center"
          align="center"
          min-width="70px"
          label="现金"/>
        <el-table-column
          prop="insuranceAmount"
          header-align="center"
          align="center"
          min-width="70px"
          label="医保金额"/>
        <el-table-column
          prop="acctFee"
          header-align="center"
          align="center"
          min-width="70px"
          label="个账金额"/>
        <el-table-column
          prop="fundFee"
          header-align="center"
          align="center"
          min-width="70px"
          label="统筹金额"/>
        <el-table-column
          prop="successTime"
          header-align="center"
          align="center"
          min-width="150px"
          label="交易时间"/>
        <el-table-column
          prop="refundAmount"
          header-align="center"
          align="center"
          min-width="85px"
          label="退款金额">
          <template #default="scope">
            <span style=" color: red;" v-if="scope.row.refundAmount !== 0">{{ scope.row.refundAmount }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="refundReason"
          header-align="center"
          align="center"
          min-width="250px"
          label="备注">
          <template #default="scope">
            <span v-if="scope.row.refundNo !== ''"> {{ scope.row.refundReason }}&nbsp;</span>
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>

  </ContentWrap>

</template>

<script lang="ts" setup>
import * as StaticsApi from '@/api/hosp/statics'
import { saveAs } from 'file-saver'
import { WorkBook, WorkSheet, utils, SSF, write } from 'xlsx'


defineOptions({ name: 'StaticsWxChsBill' })

const message = useMessage() // 消息弹窗

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const queryParams = reactive({
  searchTime: [],
  mchType: 'WX_CHS'
})
const formRules = reactive({
    searchTime: [{required: true, message: '账单日期不能为空', trigger: 'blur'}],
})
const queryFormRef = ref() // 搜索的表单

const tableHeight = ref(100)
const tableRowClassName = ({row}) => {
  if (row.title.indexOf('合计') > -1) {
    return 'my-table hj-row'
  } else if (row.title === '总计收入') {
    return 'my-table zj-row'
  }
  return 'my-table'
}


const titleFormatter = (title) => {
  return title.replace('-医保移动支付', ' ')
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 查询列表 */
const getList = async () => {
  await queryFormRef.value.validate()
  loading.value = true
  try {
    const data = await StaticsApi.getChsBill(queryParams)
    list.value = data
  } finally {
    loading.value = false
  }
}

const exportExcel = () => {
  let fix = document.querySelector('.el-table__fixed')
  let wb
  if (fix) {
    // 如果有fixed，先移除，然后再添加上
    wb = utils.table_to_book(document.querySelector('#bill-table').removeChild(fix),{ raw: true })
    document.querySelector('#bill-table').appendChild(fix)
  } else {
    wb = utils.table_to_book(document.querySelector('#bill-table'),{ raw: true })
  }
  let wbout = write(wb, {
    bookType: 'xlsx',
    bookSST: true,
    type: 'array'
  })
  try {
    saveAs(new Blob([wbout], {
      'type': 'application/octet-stream'
    }), '微信医保移动支付对账单(' + queryParams.searchTime[0] + ' 至 ' + queryParams.searchTime[1] + ').xlsx'
    )
  } catch (e) {
    if (typeof console !== 'undefined') {
      console.log(e, wbout)
    }
  }
  return wbout
}

/** 初始化 **/
onMounted(() => {
  let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
  tableHeight.value = windowHeight - 250;
})

</script>

<style scoped>


:deep(.el-table) {
  position: relative;
  overflow: hidden;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  width: 100%;
  max-width: 100%;
  font-size: 12px;
  color: #000000;
}

:deep(.zj-row) {
  background-color: #3E8EF7 !important;
}
:deep(.hj-row) {
  background-color: #36bc99 !important;
}
:deep(.my-table td) {
  padding: 5px 0 !important;
}
</style>
